package com.dmatek.uwb.packet.ianalysis.handler;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dmatek.uwb.packet.bean.ENodeInfor;
import com.dmatek.uwb.packet.bean.SocketAnchorInfor;
import com.dmatek.uwb.packet.ianalysis.callback.IReportCallBack;
import com.dmatek.uwb.util.tool.AnalyticalUtil;
import com.dmatek.uwb.util.tool.StringUtil;
/**   
 * 解析基站数据包的Handler
 * @author zhangfu  
 * @date 2019年12月3日 下午1:51:02 
 * @version V1.0   
 */
public class AnchorReportHandler extends BaseAnalysisPackHandler {
	private Map<String, SocketAnchorInfor> enodesMap = null;
	private IReportCallBack<ENodeInfor> iReportCallBack;
	public AnchorReportHandler(IReportCallBack<ENodeInfor> iReportCallBack) {
		super();
		this.enodesMap = new ConcurrentHashMap<String, SocketAnchorInfor>(AnalyticalUtil.INITIAL_CAPACITY);
		this.iReportCallBack = iReportCallBack;
	}
	@Override
	public boolean templateMethod(byte[] temps, int limit,
			InetSocketAddress sendAddress, int index) throws Exception {
		short cs = 0;
		String strId = "";
		if(temps[index + 1] == AnalyticalUtil.ROUTER_TYPE 
		&& temps[index + AnalyticalUtil.ROUTER_PACK_SIZE - 1] == AnalyticalUtil.END) { //Router上报
			int size = index + AnalyticalUtil.ROUTER_PACK_SIZE - 2;
			for (int i = index; i < size; i++) {
				cs += temps[i];
			}
			cs &= 0x00FF;
			if ((byte) cs == temps[size]) {
				strId = String.format("%02X%02X", temps[index + 2], temps[index + 3]);
				SocketAnchorInfor anchor = null;
				if (enodesMap.containsKey(strId)) {
					anchor = enodesMap.get(strId);
					if (null != anchor) {
						anchor.setSleepTime((int) ((temps[index + 4] & 0xFF) << 8 | (temps[index + 5] & 0xFF)));
						anchor.setTick(System.currentTimeMillis());
						int version = (int) (((temps[index + 6] & 0xFF) << 24)
								| ((temps[index + 7] & 0xFF) << 16)
								| ((temps[index + 8] & 0xFF) << 8) 
								| (temps[index + 9] & 0xFF));
						anchor.setVersion(Integer.toHexString(version));
						anchor.setIp(sendAddress.getAddress().getHostAddress());
						/***
						 * 新增 InetSocketAddress字段, 方便回传数据
						 */
						anchor.setInetSocketAddress(sendAddress);
						anchor.setReportTime(StringUtil.dateToString(new Date()));
						if(null != iReportCallBack) {
							iReportCallBack.callBack(anchor);
						}
					}
				} else {
					anchor = new SocketAnchorInfor();
					byte[] ids = new byte[2];
					System.arraycopy(temps, index + 2, ids, 0, 2);
					String sid = String.format("%02X%02X", ids[0], ids[1]);
					anchor.setId(sid);
					anchor.setSleepTime((int) ((temps[index + 4] & 0xFF) << 8 | (temps[index + 5] & 0xFF)));
					anchor.setTick(System.currentTimeMillis());
					int version = (int) (((temps[index + 6] & 0xFF) << 24) | ((temps[index + 7] & 0xFF) << 16)
							| ((temps[index + 8] & 0xFF) << 8) | (temps[index + 9] & 0xFF));
					anchor.setVersion(Integer.toHexString(version));
					anchor.setIp(sendAddress.getAddress().getHostAddress());
					/***
					 * 新增 InetSocketAddress字段, 方便回传数据
					 */
					anchor.setInetSocketAddress(sendAddress);
					anchor.setReportTime(StringUtil.dateToString(new Date()));
					// 设置基站讯息
					if(null != iReportCallBack) {
						iReportCallBack.callBack(anchor);
					}
					enodesMap.put(sid, anchor);
				}
				return true;
			}
		}
		return false;
	}
	/***
	 * 定义当前处理器类的数据包的包头
	 */
	@Override
	public byte getPacketHeader() {
		return AnalyticalUtil.HEAD;
	}
	public Map<String, SocketAnchorInfor> getEnodesMap() {
		return enodesMap;
	}
	@Override
	public int getPacketSize() {
		return AnalyticalUtil.ROUTER_PACK_SIZE;
	}
}
